package algorithms.leaning.class10;

import common.bean.TreeNode;
import common.util.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 递归遍历二叉树
 *
 * @author guichang
 * @date 2021/6/8
 */

@SuppressWarnings("all")
public class Code2_二叉树_递归遍历 {

    public static void main(String[] args) {
        TreeNode treeNode = MyUtil.createTreeNode(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> list = new ArrayList<>();
        recursiveTraversal(treeNode, list);
        MyUtil.printf("递归序：{}", list);

    }

    /**
     * 树递归遍历
     */
    public static void recursiveTraversal(TreeNode head, List<Integer> list) {
        if (head == null) {
            return;
        }
        // 先序
        list.add(head.val);
        recursiveTraversal(head.left, list);
        // 中序
        list.add(head.val);
        recursiveTraversal(head.right, list);
        // 后序
        list.add(head.val);
    }


}